package com.zxk.service;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.app.ContextHolder;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.util.FuzzyPicUtil;
import com.zxk.util.KeyUtil;
import com.zxk.util.MouseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 副本任务抽象父类
 *
 * @author: 舟小亢
 * @date: 2024-11-17 15:38
 */
public abstract class AbstractInstanceTask extends AbstractMyTask {
    private static Logger LOG = LoggerFactory.getLogger(AbstractInstanceTask.class);

    public AbstractInstanceTask(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    protected void afterFinishTask() throws Exception {
        LOG.info("【队长】副本任务完成，开始退出队伍");
        if (!ContextHolder.getIsQuitTeamAfterInstance().get()){
            LOG.warn("【队长】副本任务完成，未设置退出队伍，不退出");
            return;
        }
        for (int i = 0; i < 2; i++) {
            LOG.info("退出队伍，循环第【{}】次", i + 1);
            // 队长结束副本任务后处理
            // 回到长安城
            returnChangAn();
            // 退出队伍
            quitTeam();
        }
        // 通知队员结束
        ContextHolder.getInstanceTaskDone().set(true);
        LOG.info("【队长】副本任务退出队伍结束");
    }

    protected void afterFinishTaskAsMember() throws Exception {
        LOG.info("【队员】副本任务完成，开始退出队伍");
        for (int i = 0; i < 2; i++) {
            LOG.info("【队员】退出队伍，循环第【{}】次", i + 1);
            // 先初始化
            iniState(3, 3000);
            // 退出队伍
            quitTeam();
        }
        LOG.info("【队员】副本任务退出队伍结束");
    }

    private void quitTeam() throws Exception {
        // 点击队伍，点3次，防止点不到
        for (int i = 0; i < 3; i++) {
            MouseUtil.lClickRandom(hwnd_1,
                    Coord.TEAM_XY.getX(),
                    Coord.TEAM_XY.getY());
        }
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.抓鬼队伍_退出队伍, 3);
        // 退出队伍界面
        KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
    }


}
